package org.hackystat.devcathlon.ui.page.scoreboard.performerpanel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.user.User;

/**
 * Displays the top performer in a match.
 * 
 * @author Daniel Arakaki
 */
public class PerformerPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Set up lists for teams and users for display in panel. 
   * 
   * @param wicketId The id from the scoreboard page.
   * @param matchName The name of a match.
   */
  public PerformerPanel(String wicketId, final String matchName) { 
    super(wicketId);
    Integer point;
    List<User> userList = new ArrayList<User>();
    final Map<User, Integer> userScoreMap = new HashMap<User, Integer>();
    final Map<Integer, String> scoreUserMap = new HashMap<Integer, String>();
    
    List<Team> teamsList = 
      new ArrayList<Team>(MatchManager.getInstance().getMatch(matchName).getTeams());
    
    String name = "";
    
    for (Team teamData : teamsList) {
      userList.addAll(teamData.getMembers());
      userList.add(teamData.getOwner());
      for (User userData : userList) {
        point = MatchManager.getInstance().getMatch(matchName).getTotalScore(userData);
        userScoreMap.put(userData, point);
        name = userData.getFirstName().concat(" " + userData.getLastName());
        scoreUserMap.put(point, name);
      }
    }
    
    // Need to sort the scores
    //final TreeMap<User, Integer> sortedMap = new TreeMap<User, Integer>(userScoreMap);
    //List<User> userKeys = new ArrayList<User>(userScoreMap.keySet());
    List<Integer> userScores = new ArrayList<Integer>(userScoreMap.values());
    TreeSet<Integer> sortedSet = new TreeSet<Integer>(userScores);

    Object[] scoreArray = sortedSet.toArray();
    
    List<Integer> scores = new ArrayList<Integer>();
    
    int size = sortedSet.size();
    
    for (int i = size - 1; i >= 0; i--) {
      scores.add((Integer) scoreArray[i]);
    }
    
    //use a comparator for sorting
    add(new Label("matchHeader", MatchManager.getInstance().getMatch(matchName).getName()));
    
    DataView dataView = new DataView("performerList", new ListDataProvider(scores)) {

      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(Item item) {
        Integer numItem = (Integer) item.getModelObject();
        //User userItem = (User) item.getModelObject();
        //String userName = userItem.getFirstName().concat(" " + userItem.getLastName());
        
        item.add(new Label("performers", scoreUserMap.get(numItem)));
        item.add(new Label("points", numItem.toString()));
      }
      
    };
    add(dataView);
  }
}
